<?php
	require_once (dirname(__FILE__)."/../admin_conn.php");
	require_once (dirname(__FILE__)."/../admin_fun.php");
	require_once  (dirname(__FILE__)."/../admin_check.php");
	
	$action=safeData("action","get");
	
	
	$fileType=array('jpg','gif','bmp','png',".jpeg");
	$upfileDir= "../".$_SESSION["upfolder"].getSavePicPath() . "/";
	
	if(!file_exists($upfileDir)){
		mkdir($upfileDir);
	}
	
	$maxSize=1000;
	
	foreach($_FILES as $FILEa){
		
		if(!in_array(substr($FILEa['name'],-3,3),$fileType))
			$errm = "文件格式不正确1　[ <a href=# onclick=history.go(-1)>重新上传</a> ]";
		if($FILEa['size']> $maxSize*1024)
			$errm = "文件大小超过了限制　[ <a onclick=history.go(-1)>重新上传</a> ]";
		if($FILEa['error'] !=0)
			$errm = "未知错误";
		
		$targetDir= "../".$_SESSION["upfolder"].getSavePicPath() . "/";
		$targetFile=date('Ymd').time().substr($FILEa['name'],-4,4);
		$realFile=$targetDir.$targetFile;
		

		if(function_exists('move_uploaded_file')){
			move_uploaded_file($FILEa['tmp_name'],$realFile);
			if(app_watermark==1){
				imageWaterMark($targetDir.$targetFile,app_waterlocation,app_waterfont);
			}
			if($action=="xht"){
				echo "{'err':'".$errm."','msg':'".app_installdir. replaceStr($upfileDir,"../../","").$targetFile."'}";
			}
			else{
				die("<script>parent.document.getElementById('pic').value='".replaceStr($upfileDir,"../../","").$targetFile."'</script>");
			}
		}
		else{
			@copy($FILEa['tmp_name'],$realFile);
			if(app_watermark==1){
				imageWaterMark($targetDir.$targetFile,app_waterlocation,app_waterfont);
			}
			if($action=="xht"){
				echo "{'err':'".$errm."','msg':'".app_installdir. replaceStr($upfileDir,"../../","").$targetFile."'}";
			}
			else{
				die("<script>parent.document.getElementById('pic').value='".replaceStr($upfileDir,"../../","").$targetFile."'</script>");
			}
		}
	}
function imageWaterMark($groundImage,$waterPos=0,$waterText="") 
{ 
      $isWaterImage = FALSE; 
      $textFont=5;
      $textColor="#FF0000";
      $formatMsg = "暂不支持该文件格式，请用图片处理软件将图片转换为Gif、JPG、PNG格式。"; 
      if(!empty($groundImage) && file_exists($groundImage)) { //背景图片存在
          $ground_info = getimagesize($groundImage); //背景图片信息
          $ground_w      = $ground_info[0];//取得背景图片的宽 
          $ground_h      = $ground_info[1];//取得背景图片的高 

          switch($ground_info[2]) {     //取得背景图片的格式  
              case 1:$ground_im = imagecreatefromgif($groundImage);break; 
              case 2:$ground_im = imagecreatefromjpeg($groundImage);break; 
              case 3:$ground_im = imagecreatefrompng($groundImage);break; 
              default:die($formatMsg); 
          } 
      } else { 
          die("需要加水印的图片不存在！"); 
      } 
      //水印位置 
		  $temp = imagettfbbox(ceil($textFont*2.5),0,"arial.ttf",$waterText);//取得使用 TrueType 字体的文本的范围 
          //参数说明:字体大小,字体角度，文字字体，文字内容
    //    例如： Array ( [0] => 1 [1] => 2 [2] => 494 [3] => 2 [4] => 494 [5] => -28 [6] => 1 [7] => -28 ) 
          $w = $temp[2] - $temp[6]; 
          //$w=493
          $h = $temp[3] - $temp[7];
          //$h=30
          unset($temp); 
          //销毁$temp变量
          $label = "文字区域"; 
     
      if( ($ground_w<$w) || ($ground_h<$h) ) { //背景宽度/高度和水印的宽度/高度比较
          	echo "需要加水印的图片的长度或宽度比水印".$label."还小，无法生成水印！"; 
          return; 
      } 
      switch($waterPos) {
          case 0:
              $posX = ($ground_w - $w) / 2; 
              $posY = ($ground_h - $h) / 2; 
              break; 
          case 1:
              $posX = $ground_w - $w; 
              $posY = 0; 
              break; 
          case 2:
              $posX = $ground_w - $w; 
              $posY = $ground_h - $h; 
              break; 
          case 3:
              $posX = 0; 
              $posY = 0; 
              break; 
          case 4:
              $posX = 0; 
              $posY = $ground_h - $h; 
              break; 
          default:
              $posX = rand(0,($ground_w - $w)); 
              $posY = rand(0,($ground_h - $h)); 
              break;     
      } 

      imagealphablending($ground_im, true); 
          if( !empty($textColor) && (strlen($textColor)==7) ) { 
              $R = hexdec(substr($textColor,1,2)); 
              $G = hexdec(substr($textColor,3,2)); 
              $B = hexdec(substr($textColor,5)); 
          } else { 
              die("水印文字颜色格式不正确！"); 
          } 
          imagestring ( $ground_im, $textFont, $posX, $posY, $waterText, imagecolorallocate($ground_im, $R, $G, $B));         

      @unlink($groundImage); 
      switch($ground_info[2]) {
          case 1:imagegif($ground_im,$groundImage);break; 
          case 2:imagejpeg($ground_im,$groundImage);break; 
          case 3:imagepng($ground_im,$groundImage);break; 
          default:die($errorMsg); 
      } 
      if(isset($water_info)) unset($water_info); 
      if(isset($water_im)) imagedestroy($water_im); 
      unset($ground_info); 
      imagedestroy($ground_im); 
} 

?>